﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using DunSilverlight.Comm;
using DunLibrary.Dun;
using DunLibrary.Query;
using DunLibrary.Client;

namespace DunSilverlight.Views
{
    /// <summary>
    /// </summary>
    public partial class EndTask : Page
    {
        private DunLibrary.Query.QueryCondition queryCondition;

        private const int Paging_Max_Result = 19;

        public EndTask()
        {
            InitializeComponent();
            InitCmbBank();
        }
        void InitCmbBank()//大写开头！
        {
            try
            {
                Action<IList<DunLibrary.Client.Company>> getBanks = (IList<DunLibrary.Client.Company> banks) =>
                {
                    cmbBank.ItemsSource = banks;
                };

                AppGlobal.GetBanks(getBanks);

            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }
        }
        
        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void btnquery_Click(object sender, RoutedEventArgs e)
        {
            if (!RightManagement.VerifyRight(RightManagement.Right_Finish_Case))
            {
                Tools.ShowMessage("您没有结案的权限！", "", false);
                return;
            }

            if (!QueryVerify())
                return;
            BuildHql();
            QueryPaging(queryCondition);

        }
        private bool QueryVerify()
        {

            if (cmbBank.SelectedItem == null)
            {
                Tools.ShowMessage("请选择银行!", "", true);

                return false;
            }

            return true;
        }

        private void btnEndCase_Click(object sender, RoutedEventArgs e)
        {


            CaseFinish(true);

        }

        private void btnOpenCase_Click(object sender, RoutedEventArgs e)
        {
            CaseFinish(false);
        }
        /// <summary>
        /// 建立hql查询条件
        /// </summary>
        private void BuildHql()
        {
            string hqlwhere = "";
            
            if (cmbBank.SelectedItem != null)
            {
                hqlwhere += " where ccl.Bank.Id=" + (cmbBank.SelectedItem as Company).Id;
            }

            queryCondition = new DunLibrary.Query.QueryCondition();

            queryCondition.HQL = "from CaseCatalog as ccl " + hqlwhere;
            queryCondition.TotalHQL = "select count(*) from CaseCatalog as ccl " + hqlwhere;

            queryCondition.MaxResults = Paging_Max_Result;
        }
        private void QueryPaging(DunLibrary.Query.QueryCondition qc)
        {
            if (qc == null || string.IsNullOrEmpty(qc.HQL))
                return;

            CaseCatalogService.CaseCatalogServiceClient ser = new CaseCatalogService.CaseCatalogServiceClient();

            ser.GetListByHQLWithPagingCompleted += (object sender, CaseCatalogService.GetListByHQLWithPagingCompletedEventArgs e) =>
            {
                try
                {
                    int totalCount = 0;

                    IList<CaseCatalog> caseCatas = JsonSerializerHelper.JsonToEntities<CaseCatalog>(e.Result, out totalCount);

                    rDataPager.NumericButtonCount = totalCount / Paging_Max_Result;//分页好像不对

                    viewEndCase.ItemsSource = caseCatas;
                }
                catch (Exception ex)
                {
                    Tools.ShowMessage(ex.Message, "", false);
                }
                finally
                {
                    ser.CloseAsync();
                }
            };

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(qc);

            ser.GetListByHQLWithPagingAsync(json);
        }
        private void CaseFinish(bool check)
        {
            if (!RightManagement.VerifyRight(RightManagement.Right_Finish_Case))
            {
                Tools.ShowMessage("您没有结案的权限！", "", false);
                return;
            }


            if (viewEndCase.SelectedItems.Count == 0)
            {
                Tools.ShowMessage("没有选择案件！", "", false);
                return;
            }
            try
            {

                IList<int> selIds = viewEndCase.SelectedItems.Select(p => (p as CaseCatalog).Id).ToList<int>();

                CaseCatalogService.CaseCatalogServiceClient ser = new CaseCatalogService.CaseCatalogServiceClient();

                ser.CaseFinishCompleted += (object sender1, CaseCatalogService.CaseFinishCompletedEventArgs e1) =>
                {
                    QueryPaging(queryCondition);
                    ser.CloseAsync();
                };
                string strIds = Newtonsoft.Json.JsonConvert.SerializeObject(selIds);

                ser.CaseFinishAsync(strIds, check);
            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }

        }

    }

}
